<template>
  <el-dialog
    :title="{ add: '新增', edit: '修改' }[mode]"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="角色名称" label-width="100px" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" label-width="100px" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRoleAdd, sysRoleEdit } from '@/api/setting'
export default {
  data() {
    return {
      mode: '',
      isShow: false,
      form: {
        name: '', // string	必须		角色名称
        description: '' // string	必须		角色描述
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    //   全局表单验证
    submmit() {
      this.$refs.form.validate(async(results) => {
        if (results) {
          if (this.mode === 'add') {
            // 新增
            const res = await sysRoleAdd(this.form)
            // 提示成功
            this.$message.success(res.message)
          } else if (this.mode === 'edit') {
            await sysRoleEdit(this.form)
            // 提示成功
            this.$message.success('修改成功')
          }
          // 关闭弹框
          this.isShow = false
          // 刷新数据
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      // 清空表单数据与表单验证
      // 重置表单清除表单验证
      this.$refs.form.resetFields()
      this.form = {
        name: '', //  string  必须    角色名称
        description: '' //  string  必须    角色描述
      }
    }
  }
}
</script>

<style></style>
